<?php
$title = 'Titres de Transport';
$this->headTitle($title);
?>
<div class="content-head">
    <h3><?php echo $title; ?></h3>
    <div class="toolbar">
        <div class="toggle add" title="Ajouter">
            <a href="#" style="display: block; width: 100%; height: 100%" onclick="$('.add-toggle').toggle('slow');
		    return false;"></a>
        </div>
    </div>
</div>
<div class="content-body">
    <div id="msg" class="alert hide"></div>
    <div class="add-toggle">
        <div class="list-container">
	    <div class="list-container">
		<table class="table table-striped list-table">
		    <thead>
			<tr>
			    <td width='35%'>Libellé</td>
			    <td width='9%'>Valeur</td>
			    <td width='8%'>Activité</td>
			    <td width='40%'>Description</td>
			    <td width='8%'>&nbsp;</td>
			</tr>
		    </thead>
		    <tbody>
			<tr>
			    <td>
				<a href="#" id="new-libelle" data-type="text"></a>
			    </td>
			    <td>
				<a href="#" id="new-valeur" data-type="text"></a>
			    </td>
			    <td>
				<a href="#" id="new-activite" data-type="select">Actif</a>
			    </td>
			    <td>
				<a href="#" id="new-description" data-type="textarea"></a>
			    </td>
			    <td style='text-align: center;' id='controls-0'>
				<input type="submit" id="add-transport" value="Ajouter" class="btn btn-success" onclick="add();">
			    </td>
			</tr>
		    </tbody>
		</table>
	    </div>
        </div>
    </div>
    <div class="list-container">
        <table class="table table-striped list-table">
            <thead>
                <tr>
                    <td width='35%'>Libellé</td>
                    <td width='9%'>Valeur</td>
                    <td width='8%'>Actif</td>
                    <td width='40%'>Description</td>
                    <td width='8%'>&nbsp;</td>
                </tr>
            </thead>
            <tbody>
		<?php foreach ($this->transportList as $transport): ?>
    		<tr>
    		    <td>
			<a id='libelle-<?php echo $transport->id; ?>' onclick='return false;' class="edit-libelle" data-name="libelle" data-pk="<?php echo $transport->id; ?>" data-type='text'>
				<?php echo $transport->libelle; ?>
    			</a>
    		    </td>
    		    <td>
			<a id='valeur-<?php echo $transport->id; ?>' onclick='return false;' class="edit-valeur" data-name="valeur" data-pk="<?php echo $transport->id; ?>" data-type='text'>
				<?php echo $transport->valeur; ?>
    			</a>
    		    </td>
    		    <td>
			<a id='actif-<?php echo $transport->id; ?>' onclick='return false;' class="edit-activite" data-name="actif" data-pk="<?php echo $transport->id; ?>" data-type='select'>
				<?php echo (int) $transport->actif == 1 ? 'Actif' : 'Inactif'; ?>
    			</a>
    		    </td>
    		    <td>
			<a id='description-<?php echo $transport->id; ?>' onclick='return false;' class="edit-description" data-name="description" data-pk="<?php echo $transport->id; ?>" data-type='textarea'>
				<?php echo $transport->description; ?>
    			</a>
    		    </td>
    		    <td style='text-align: center;' id='controls-<?php echo $transport->id ?>'>
    			<a class='grh-delete' href='#' onclick="return erase(<?php echo $transport->id ?>);">
    			    <img src='<?php echo $this->basePath() . '/img/grh/delete-16x16.png' ?>' alt='Effacer' title='Effacer'>
    			</a>
    		    </td>
    		</tr>
		<?php endforeach; ?>
            </tbody>
        </table>
    </div>
</div>

<form id="newTransport" action="#" method="post">
    <input type="hidden" id="data-id" name="id" value="0">
    <input type="hidden" id="data-libelle" name="libelle" value="">
    <input type="hidden" id="data-valeur" name="valeur" value="0">
    <input type="hidden" id="data-actif" name="actif" value="1">
    <input type="hidden" id="data-description" name="description" value="">
</form>

<script type='text/javascript'>
// Démarrage
		$(function() {
		    // Nouveau
		    $('#new-libelle').editable({
			title: "Nouveau Libellé",
			validate: function(value){
			    $('#data-libelle').val(value);
			    isValid();
			}
		    });
		    $('#new-valeur').editable({
			title: 'Novuelle Valeur',
			validate: function(value){
			    $('#data-valeur').val(value);
			    isValid();
			}
		    });
<?php $activite = array(1	 => 'Actif', 0	 => 'Non Actif'); ?>
		    $('#new-activite').editable({
			title: 'Actif/Non Actif',
			source: <?php echo json_encode($activite); ?>,
			validate: function(value){
			    $('#data-activite').val(value);
			    isValid();
			}
		    });
		    $('#new-description').editable({
			title: 'Description',
			validate: function(value){
			    $('#data-description').val(value);
			    isValid();
			}
		    });
		    // Edition
		    $('.edit-libelle').editable({
			title: 'Edition du libellé',
			url : '<?php echo $this->url('RHX/default', array('controller'	 => 'transport', 'action'	 => 'saveStringData')) ?>'
		    });
		    $('.edit-valeur').editable({
			title: 'Edition de la valeur',
			url : '<?php echo $this->url('RHX/default', array('controller'	 => 'transport', 'action'	 => 'saveStringData')) ?>',
			tpl : "<input type='number' onclick='select();'>"
		    });
		    $('.edit-activite').editable({
			title: 'Edition de l\'activité',
			url : '<?php echo $this->url('RHX/default', array('controller'	 => 'transport', 'action'	 => 'saveIntData')) ?>',
			source : '<?php echo json_encode($activite); ?>'
		    });
		    $('.edit-description').editable({
			title: 'Edtion de la description',
			url : '<?php echo $this->url('RHX/default', array('controller'	 => 'transport', 'action'	 => 'saveStringData')) ?>'
		    });
		});

		function erase(id)
		{
		    var conf = confirm("Voulez-vous vraiment effacer le Titre de transport ?");
		    if (conf != true)
			return false;
		    var urlDelete = "<?php echo $this->url('RHX/default', array('controller'	 => 'transport', 'action'	 => 'erase')) ?>";
		    var ajax = $.ajax({
			type: 'post',
			url: urlDelete,
			data: '&id=' + id
		    });
		    ajax.success(function(result) {
			if(result.done){
			}else{
			    alert("Erreur");
			}
			location.reload();
		    });
		    ajax.error(function() {
			alert('Une erreur est survenue');
		    });
		    return false;
		}

		function add(){
		    if(!isValid(true)){
			return false;
		    }
		    ajax = $.ajax({
			type: 'post',
			url : '<?php echo $this->url('RHX/default', array('controller'	 => 'transport', 'action'	 => 'add')) ?>',
			data: $('#newTransport').serialize()
		    });
		    ajax.done(function(result){
			if(!result.done){
			    alert("Erreur lors de l'ajout !");
			}
			window.location.reload();
		    });
		}
		function isValid(submit){
		    libelle = $('#data-libelle').val();
		    if(libelle.length > 3){
			// valide
			$('#add-transport').show('normal');
			return true;
		    }else{
			// Invalide
			$('#add-transport').hide('fast');
			if(submit){
			    alert("Le libellé est trop court");
			}
			return false;
		    }
		}
</script>